package com.java.controller;

import com.alibaba.fastjson.JSONArray;
import com.java.pojo.EnterGoods;
import com.java.pojo.Maker1;
import com.java.pojo.Maker2;
import com.java.pojo.User;
import com.java.service.EnterGoodsService;
import com.java.service.Maker1Service;
import com.java.service.Maker2Service;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/Maker2Controller")
public class Maker2Controller {

    @Autowired
    private Maker2Service maker2Service;

    @Autowired
    private EnterGoodsService enterGoodsService;


    @Autowired
    private Maker1Service maker1Service;

    @RequestMapping("/findAllByMaker2")
    public String findAllByMaker2(
            @RequestParam(value = "rid",required = false) Integer rid,
            @RequestParam(value = "goodstyle",required = false) Integer goodstyle,
            @RequestParam(value = "mid",required = false) Integer mid,
            Model model
    ){
        List<Maker2> list = maker2Service.getMaker2ListByM1id(mid);
        System.out.println("mid:"+mid);
        System.out.println(list);
        model.addAttribute("list",list);
        model.addAttribute("rid",rid);
        model.addAttribute("goodstyle",goodstyle);
        model.addAttribute("mid",mid);
        return "maker2/findAllByMaker2";
    }

    /*添加页面*/
    @RequestMapping("/priAddMaker2")
    public String priAddMaker2(
            @RequestParam(value = "rid",required = false) Integer rid,
            @RequestParam(value = "m1id",required = false) Integer m1id,
            @RequestParam(value = "goodstyle",required = false) Integer goodstyle,
            Model model
    ){
        /*根据goodstyle查询entergoods的内容*/
        List<EnterGoods> goods = enterGoodsService.getGoodsByGoodStyle(goodstyle);
        model.addAttribute("goods",goods);
        model.addAttribute("rid",rid);
        model.addAttribute("m1id",m1id);
        model.addAttribute("goodstyle",goodstyle);
        return "maker2/AddMaker2";
    }


    //添加
    @RequestMapping(value = "/AddMaker22",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String AddMaker22(
            @RequestParam(value = "rid",required = false) Integer rid,
            @RequestParam(value = "m1id",required = false) Integer m1id,
            @RequestParam(value = "goods2",required = false) String goods2,
            @RequestParam(value = "num",required = false) Integer num,
            @RequestParam(value = "onemoney",required = false) String onemoney,
            @RequestParam(value = "goodstyle",required = false) Integer goodstyle,
            @RequestParam(value = "danwei",required = false) String danwei,
            @RequestParam(value = "discount",required = false) String discount
    ){
        //修改entergoods的okandno3==1,已经在销售订单当中
        int i2 = enterGoodsService.updOknandNo3(goods2);

        System.out.println(discount+"---------------------");
        Double dis=Double.parseDouble(discount);
        Double dis2=dis/10;
        System.out.println("dis2:"+dis2+",dis:"+dis);
        Maker2 maker2=new Maker2();
        double v = Double.parseDouble(onemoney);
        System.out.println("v:"+v);
        /*String转换为Double*/

        //折扣后的单价
        double oneMoney = dis2 * v;
        System.out.println("oneMoney:"+oneMoney);
        /*折扣后的总价格*/
        double disCountMoney = oneMoney * num;
        System.out.println("disCountMoney:"+disCountMoney);
        /*没有折扣的总价格*/
        Double CountMoney = v * num;
        System.out.println("CountMoney:"+CountMoney);
        /*编号*/
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyyMMdd");
        String newDate=sdf2.format(new Date());
        String result="";
        String s="CP";
        Random random=new Random();
        for(int i=0;i<3;i++){
            result+=random.nextInt(10);
        }
        System.out.println(s+newDate+result);
        //编号
        String document2=s+newDate+result;
        maker2.setM1id(m1id);
        maker2.setRid(rid);
        maker2.setGoods2(goods2);
        maker2.setNum(num);
        maker2.setOnemoney(v);
        maker2.setGoodstyle(goodstyle);
        maker2.setDanwei(danwei);
        maker2.setDiscount(Integer.parseInt(discount));
        maker2.setDocuemnt2(document2);
        maker2.setNowmoney(oneMoney);
        maker2.setDiscountmoney(disCountMoney);
        maker2.setCountmoney(CountMoney);
        System.out.println("maker2:"+maker2);

        /*根据外键查询*/
        Maker1 maker1 = maker1Service.getMaker1ByMid(m1id);
        String goodss=maker1.getGoodname();
        Integer numm=maker1.getNum();
        Double moneyyy=maker1.getMoney();

        if("".equals(goodss)||null==goodss){
            goodss=" ";
        }
        if(numm==null ||"".equals(numm)){
            numm=0;
        }
        if(moneyyy==null ||"".equals(moneyyy)){
            moneyyy=0.0;
        }

        /*赋值*/
        StringBuffer sb=new StringBuffer(goodss);
        if("".equals(sb)){
            StringBuffer append = sb.append(goods2);
        }
        StringBuffer append = sb.append("," + goods2);
        String nwGoodName=append.toString();

        Integer num2=numm+num;
        Double money2=moneyyy+disCountMoney;
        maker1.setGoodname(nwGoodName);
        int i1 = maker1Service.updateMaker(m1id, num2, money2, nwGoodName);


        /*添加操作*/
        int i = maker2Service.ADDMaker2(maker2);
        if(i>0&&i1>0&&i2>0){
            return "<script>alert('添加成功欢迎回来');location.href='/Maker1/findAllByPage';</script>";
        }else {
            return "<script>alert('添加失败');location.href='/Maker1/findAllByPage';</script>";
        }
    }


    /*删除*/
    @RequestMapping(value = "/del",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String DEL(@RequestParam("m2id") Integer m2id){
        System.out.println(m2id);
        int i = maker2Service.deleteMaker2(m2id);
        List<Maker2> list = maker2Service.getMaker2List(m2id);
        for (int j=0;j<list.size();j++){
            System.out.println(list.get(j).getGoods2());
            enterGoodsService.updOknandNo3_2(list.get(j).getGoods2());
        }
        if(i==2){
            return "<script>alert('删除successful');location.href='/Maker1/findAllByPage';</script>";
        }else {
            return "<script>alert('删除失败!');location.href='/Maker1/findAllByPage';</script>";
        }
    }


    /*修改页面*/
    @RequestMapping(value = "/UPD",produces = "text/html;charset=UTF-8")
    public String UPD(@RequestParam("m2id") Integer m2id,Model model){
        System.out.println(m2id);
        Maker2 maker2 = maker2Service.getMak2ByM2id(m2id);
        System.out.println(maker2+"[[[]]]");
        if(maker2!=null){
            model.addAttribute("m2",maker2);
            return "maker2/updMaker2";
        }
        return "error";
    }


    /*修改*/
    @RequestMapping(value = "/UPD2",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String UPD(Maker2 maker2){
        int i = maker2Service.updMaker2(maker2.getM2id(), maker2.getNum(), maker2.getOnemoney(), maker2.getDiscount().toString());
        if(i==2){
            return "<script>alert('修改successful');location.href='/Maker1/findAllByPage';</script>";
        }else {
            return "<script>alert('修改失败!');location.href='/Maker1/findAllByPage';</script>";
        }
    }
//        System.out.println(m2id);
//        int i = maker2Service.deleteMaker2(m2id);
//        if(i==2){
//            return "<script>alert('删除successful');location.href='/Maker1/findAllByPage';</script>";
//        }else {
//            return "<script>alert('删除失败!');location.href='/Maker1/findAllByPage';</script>";
//        }
//    }

    @RequestMapping("/findGoods2")
    @ResponseBody
    public String findGoods2(@RequestParam("goods2") String goods2,@RequestParam("num") String num){
        HashMap<String,String> map=new HashMap<>();
        EnterGoods enByName = enterGoodsService.findEnByName(goods2);
        System.out.println("en"+enByName);
        if(Integer.parseInt(num)<=enByName.getNum()){
            map.put("userCode","exist");
        }else{
            map.put("userCode","no");
        }
        //将map转为json
        return JSONArray.toJSONString(map);
    }
}
